import Vue from "vue";
import VueRouter from "vue-router";

import Login from "@/components/login";
import Home from "@/components/home";
import Welcome from "@/components/welcome";
import Users from "@/components/users";
import Roles from "@/components/roles/roles";
import Rights from "@/components/roles/rights";
import Categories from "@/components/goods/categories";
import Params from "@/components/goods/params";
import Goods from "@/components/goods/goods";
import Add from "@/components/goods/add";
import Orders from "@/components/orders";
import Reports from "@/components/reports";

Vue.use(VueRouter);

const routes = [
  { path: "/", redirect: "/login" },
  { path: "/login", component: Login },
  { path: "/home", component: Home, children: [
    { path: '/home', redirect: '/welcome' },
    { path: '/welcome', component: Welcome },
    { path: '/users', component: Users },
    { path: '/roles', component: Roles },
    { path: '/rights', component: Rights },
    { path: '/categories', component: Categories },
    { path: '/params', component: Params },
    { path: '/goods', component: Goods },
    { path: '/goods/add', component: Add },
    { path: '/orders', component: Orders },
    { path: '/reports', component: Reports }
  ] },
];

const router = new VueRouter({
  routes,
});

// 路由守卫
router.beforeEach((to, from, next) => {
  if (to.path == "/login") window.sessionStorage.token ? next("/home") : next();
  if (!window.sessionStorage.token) return next("/login");
  next();
});

export default router;
